package com.wm.machine.baselibrary.utils.net.core;

import java.io.File;
import java.lang.reflect.Proxy;

import io.rx_cache2.internal.RxCache;
import io.victoralbertos.jolyglot.GsonSpeaker;

/**
 * Created by wentong.chen on 18/2/10.
 * 功能：
 */

public class NetCacheUtil {

    public static  <S> S getCacheNet(File cacheFile, Class<S> cacheProvider) {
        if (cacheFile == null) {
            throw new IllegalArgumentException("file cant be null or arg is not file");
        }
        S s = new RxCache.Builder()
                .persistence(cacheFile, new GsonSpeaker())
                .using(cacheProvider);
        //添加请求数据时指定io线程的代理操作
        S provider = (S) Proxy.newProxyInstance(cacheProvider.getClassLoader(),
                new Class[]{cacheProvider}, new NetInvocationHandler<>(s));
        return provider;
    }
}
